package com.scedu.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/index")
//@Slf4j
public class MyController {
    @GetMapping("/hello")
    public ModelAndView toIndex(Map<String,Object> map){
        System.out.println("HELLO");
        map.put("msg", "hello shiro");
        return new ModelAndView("index", map);

    }

    @GetMapping("/add")
    public String toAdd(Map<String,Object> map){
        System.out.println("add");

        return ("/user/add");

    }

    @GetMapping("/edit")
    public String toEdit(Map<String,Object> map){
        System.out.println("edit");

        return ("/user/edit");

    }
    @GetMapping("/login")
    public String toLogin(Map<String,Object> map){
        System.out.println("to login");

        return ("/login");

    }
    @RequestMapping("/dologin")
    public ModelAndView dologin(String username,String password,Map<String,Object> map){
        System.out.println("username="+username);
        //1.获取当前用户对象
        Subject currentUser = SecurityUtils.getSubject();
        //2.封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //3.执行登录方法
        try {
            System.out.println("1.token="+token.hashCode());
            currentUser.login(token);
            //登录成功
            map.put("msg", "hello shiro");
            return new ModelAndView("index", map);
        } catch (UnknownAccountException uae) {
            System.out.println("There is no user with username of " + token.getPrincipal());
            map.put("msg","There is no user with username of " + token.getPrincipal());
           // log.info("There is no user with username of " + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            System.out.println("111");
            map.put("msg","Password for account " + token.getPrincipal() + " was incorrect!" );
            //log.info("Password for account " + token.getPrincipal() + " was incorrect!");
        } catch (LockedAccountException lae) {
            System.out.println("222");

            map.put("msg","error " );

            //  log.info("The account for username " + token.getPrincipal() + " is locked.  " +
             //       "Please contact your administrator to unlock it.");
        }
        // ... catch more exceptions here (maybe custom ones specific to your application?
        catch (AuthenticationException ae) {
            System.out.println("333");
            map.put("msg","error " );

            //unexpected condition?  error?
        }
        System.out.println(map);
        return new ModelAndView("login", map);
    }

    //未授权
    @GetMapping("/noauth")
    @ResponseBody
    public String Unauthorized(){
        return ("未授权用户无法访问！");
    }
}
